$(function(){
    var parent = $('select[name="parent"]'),
    success = function(data){
	var options = [];
	$(data.records).each(function(i, item){
	    options.push('<option value="'+item.id+'">'+item.menu_zh+' / '+item.menu_fr+' / '+item.menu_en+'</option>');
	});
	parent.html(options.join(''));
	if(typeof PARENT != 'undefined'){
	    parent.val(PARENT);
	}
    },
    setParent = function(type){
	switch(type){
	    case '1':
	    case '0':
		//首页
		parent.html('<option value="0">--</option>');
		break;
	    case '2':
		//关于
		Z.ajax({
		    url : Z.url('/backend/about/records'),
		    success : success
		});
		break;
	    case '3':
		//服务
		Z.ajax({
		    url : Z.url('/backend/service/records'),
		    success : success
		});
		break;
	}
    };
    
    new Z.ImageBrowser($('input[name="image"]'));

    if(typeof PARENT != 'undefined'){
	var type = $('select[name="type"]').val();
	setParent(type);
    }
    
    $('select[name="type"]').change(function(){
	setParent($(this).val());
    });
    
    $('form').submit(function(){
	Z.ajax({
	    url : $(this).attr('action'),
	    type: $(this).attr('method'),
	    data: $(this).serialize()
	});
	return false;
    });
});